package ncku.account.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import ncku.account.event.Launcher;

/*本程式中,各java檔所共用的variable
 * ArrayList<Category> categories 分類
 * ArrayList<Transaction> transactions 交易細目
 * String accountName 帳戶名稱 
*/
public class GlobalData {
	public static ArrayList<Category> categories;
	public static ArrayList<Transaction> transactions;
	public  static String accountName = "null";
	public	static int accountMoney;
	public  static int balanceMoney;  //帳戶餘額
	public  static boolean isAlert;
	public  static int alertMoney;
	
	public GlobalData(){
		categories = new ArrayList<Category>();
		transactions = new ArrayList<Transaction>();
		isAlert = false;
		alertMoney = 0;
	}
	
	//檢查subTransaction中的amount總和,與Transaction中的amount是否相符
	//若差額!=0 or !=amount,則新增一筆subTransaction : Remainder $diff
	public static ArrayList<SubTransaction> checkSumOfAmount(int amount, SubTransactionList subTransList){
		int sum = 0;
		int diff = 0;
		
		for(int i=0; i< subTransList.getSize(); i++){
			sum += subTransList.getSubTrans(i).getSubAmount();
		}
		
		diff = amount - sum;
		if((diff!=0) && (diff!=amount)){  //金額不符,要新增一條subTransaction
			subTransList.addSubTrans("Remainder", diff);
		}
	
		return subTransList.getArrayListSubTrans();
	}
	
	//將new完的transaction加入ArrayList中
	public static void add(Date date, String description, int amount, Category category, 
						   ArrayList<SubTransaction> subTransaction){
		Transaction trans = new Transaction(date, description, amount, category, subTransaction);
		transactions.add(trans);
		//categories.add(category);
		UpdateBalanceMoney(amount);
	}
	
	//修改Transaction
	public static void edit(int index, Date date, String description, int amount, Category category,
							ArrayList<SubTransaction> subTransaction){
		int oldAmount = getTransaction(index).getAmount();
		UpdateBalanceMoney(-oldAmount);
		Transaction trans = new Transaction(date, description, amount, category, subTransaction);
		transactions.set(index,trans);
		//categories.set(index,category);
		UpdateBalanceMoney(amount);
	}
	
	//Delete Transaction
	public static void delete(int index){
		int oldAmount = getTransaction(index).getAmount();
		UpdateBalanceMoney(-oldAmount);
		transactions.remove(index);
	}
	
	//Add Category
	public static void add(String name){
		Category newCate = new Category(name);
		categories.add(newCate);
	}
	
	//Edit Category
	public static void edit(String name, int index){
		categories.get(index).setName(name);
	}
	
	//Delete Category
	public static void deleteCategory(int index){
		categories.remove(index);
	}
	
	//更新帳戶餘額
	public static void UpdateBalanceMoney(int amount){
		balanceMoney += amount;
		if(isAlert){
			String alert = "帳戶餘額已低於" + String.valueOf(alertMoney) + "元";
			JOptionPane.showMessageDialog(Launcher.frame, alert, "提示訊息", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public static boolean alertCheck(){
		if(balanceMoney < alertMoney){
			return true;
		}
		else{
			return false;
		}
	}
	
	public static Transaction getTransaction(int index){
		return transactions.get(index);
	}
	
	public static Category getCategory(int index){
		return categories.get(index);
	}
	
	//讀取transaction總筆數
	public static int getTransactionNumber(){
		return transactions.size();
	}
	
	//讀取category總筆數
	public static int getCategoryNumber(){
			return categories.size();
	}
		
	public static ArrayList<Transaction> getTranForExport(){
		return transactions;
	}
	
	public static ArrayList<Category> getCategoryForExport(){
		return categories;
	}
	
	//將舊的交易list 覆蓋過去，用在import 的時候
	public static void loadAccountImformationTransaction(ArrayList<Transaction> loadAccount){
		transactions = loadAccount;
	}
	//將舊的交易list 覆蓋過去，用在import 的時候
	public static void loadAccountImformationCategory(ArrayList<Category> loadAccount){
		categories = loadAccount;
	}
}

